<?php

namespace App\Http\Controllers\H5;

use App\Models\ItemCatlog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends BaseController
{
    public function index()
    {
        $categories = ItemCatlog::where('fid', 0)->orderBy('displayorder')->get(['catid', 'name']);
        return $this->view('h5.category', compact('categories'));
    }

    public function getJson()
    {
        $fid = $this->request->input('fid');
        if ($fid){
            $category = ItemCatlog::find($fid);
        } else {
            $category = ItemCatlog::where('fid', 0)->orderBy('displayorder')->first();
        }

        $items = $category->childs()->get(['catid', 'name', 'icon'])->map(function ($catlog){
            $catlog->icon = image_url($catlog['icon']);
            return $catlog;
        });
        return ajaxReturn(['items'=>$items]);
    }
}
